package com.loren.demo08.controller;

import com.loren.demo08.example01.GrantType;
import com.loren.demo08.example01.OAuthPatternFactory;
import com.loren.demo08.example02.DeviceFactory;
import com.loren.demo08.example03.SmsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class TestController {

    private final SmsService smsService;

    private final OAuthPatternFactory oauthPatternFactory;

    @GetMapping("/demo01")
    public String demo01(@RequestParam("grant_type") String params) {
        Map<String, Object> mapInfo = new HashMap<>();
        mapInfo.put("grantType", params);
        try {
            GrantType grantType = GrantType.valueOf(params);
            this.oauthPatternFactory.getOAuthPattern(grantType).handle(mapInfo);
        } catch (IllegalArgumentException e) {
            return "unrecognized parameter value, " + params;
        }
        return "success";
    }

    @GetMapping("/demo02")
    public String demo02(@RequestParam("device") String device) {
        DeviceFactory.getGameDevice(device).play("超级马力欧");
        return "success";
    }

    @GetMapping("/demo03")
    public String demo03(@RequestParam("plugin") String plugin) {
        this.smsService.sendSmsMessage(plugin);
        return "success";
    }
}
